package edu.Sort;

import java.util.Arrays;
import java.util.Scanner;

public class Demo1 {
    public static int[] sort1(int[] arr){

        for (int i = 0; i <arr.length ; i++) {
            for (int j = 0; j <arr.length-i-1 ; j++) {
                if(arr[j] > arr[j+1]) {//升序
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for (int i : arr) {
            System.out.println(i);
        }
        return arr;
    }

    public static int[] sort2(int[] arr){
        for (int i = 0; i <arr.length ; i++) {
            for (int j = 0; j <arr.length-i-1 ; j++) {
                if(arr[j] < arr[j+1]) {//降序
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for (int i : arr) {
            System.out.println(i);
        }
        return arr;
    }

    public static void main(String[] args) {

        System.out.println ("你将要排序几个数：");
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int[] arr = new int[a];
        System.out.println("请依次输入数据：");
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入第"+(i+1)+"个数：");
            arr[i]=scan.nextInt();
        }
        System.out.println("升序排序结果如下：");
        System.out.println(Arrays.toString(sort1(arr)));
        System.out.println("降序排序结果如下：");
        System.out.println(Arrays.toString(sort2(arr)));

    }
}
